package dp;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class maxProfit4 {
    public int maxProfit(int k, int[] prices) {
        int len = prices.length;
        int s = 2 * k;
        int[][] dp = new int[len][s + 1];
        for (int i = 0; i <= s; i++) {
            if (i % 2 != 0){
                dp[0][i] = -prices[0];
            }
        }
        for (int i = 1; i < prices.length; i++) {
            for (int j = 1; j <= s; j++) {
                if(j % 2 == 0){
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] + prices[i]);
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] - prices[i]);
                }
            }

        }
        return dp[len - 1][s];
    }
}
